package com.lvy.hczn.front.business.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.business.common.CacheConstants;
import com.lvy.hczn.front.business.common.Constants;
import com.lvy.hczn.front.system.domain.business.OutboundOrderDetail;
import com.lvy.hczn.front.business.dto.OutboundOrderDetailDto;
import com.lvy.hczn.front.business.dto.Param;
import com.lvy.hczn.front.business.service.OutboundOrderDetailService;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.common.exception.UtilException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/wcs/out/detail/")
@Slf4j
public class OutboundOrderDetailController {

    @Autowired
    private OutboundOrderDetailService outboundOrderDetailService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 根据取货单查询
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "根据取货单查询", notes = "根据取货单查询")
    @PostMapping("selectByCode")
    public ResultUtil selectByCode(@Valid @RequestBody Param param) {
        try {
            String outboundOrder = param.getUpOrDownPolicy().getOutboundOrderCode();
            if (StrUtil.isEmpty(outboundOrder)) {
                return ResultUtil.failed("出库编号为空");
            }
            OutboundOrderDetail detail = outboundOrderDetailService.getOne(Wrappers.<OutboundOrderDetail>lambdaQuery().eq(OutboundOrderDetail::getOutboundOrderCode, outboundOrder).eq(OutboundOrderDetail::getStatus, Constants.OUTBOUND_DETAIL_STATUS_ZERO).last("limit 1"));
            if (detail == null) {
                return ResultUtil.failed("请输入正确的货箱编号");
            }

            String key = CacheConstants.REDIS_KEY_SOCKET_CLIENT + ":" + param.getUpOrDownPolicy().getClientId() + ":outboundCode";
            if (redisCache.get(key) == null) {
                redisCache.set(key, outboundOrder);
            }

            return ResultUtil.ok(detail);
        } catch (Exception e) {
            log.error("selectByCode:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    /**
     * 出库统计
     *
     * @param param
     * @return
     * @author yw
     * @date 2022-04-07 13:03:42
     */
    @ApiOperation(value = "出库统计", notes = "出库统计")
    @PostMapping("outboundCount")
    public ResultUtil outboundCount(@RequestBody Param param) {
        try {
            List<OutboundOrderDetailDto> list = outboundOrderDetailService.outboundCount(param.getUpOrDownPolicy().getOutboundOrderCode());
            return ResultUtil.ok(list);
        } catch (Exception e) {
            log.error("outboundCount:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }
}
